﻿Public Class ResourceForm
    Private parentForm As SearchResourcesForm = New SearchResourcesForm

    Enum state
        insert
        edit
    End Enum

    Dim action As state = state.insert

    Public Sub ResourceForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        GenericAdministrator.loadComboBox(Me.cmbDocumentType, GenericDao.searchData("Document_Type", "*"), "id", "description")
        If parentForm.idSelected <> Nothing Then
            loadFormToEdit(parentForm.idSelected)
            action = state.edit
            Me.txtDocument.Enabled = False
            Me.cmbDocumentType.Enabled = False
        End If
    End Sub

    Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        If Me.validateRequiredFields() = True Then
            If Me.action = state.insert Then
                If Me.validateDuplicatedResource = True Then
                    ResourceAdministrator.insert(Me.txtFirstname.Text, Me.txtSurname.Text, Me.cmbDocumentType.SelectedValue, Me.txtDocument.Text, Me.txtEmail.Text, Me.txtHourCost.Text)
                    Me.Close()
                    Dim searchResourcesForm As New SearchResourcesForm()
                    searchResourcesForm.Show()
                End If
            Else
                ResourceAdministrator.update(parentForm.idSelected, Me.txtFirstname.Text, Me.txtSurname.Text, Me.cmbDocumentType.SelectedValue, Me.txtDocument.Text, Me.txtEmail.Text, Me.txtHourCost.Text)
                Me.Close()
                Dim searchResourcesForm As New SearchResourcesForm()
                searchResourcesForm.Show()
            End If

        End If
    End Sub

    Public Sub New()
        InitializeComponent()
    End Sub

    Public Sub New(ByRef parentFormParam As SearchResourcesForm)
        InitializeComponent()
        parentForm = parentFormParam
    End Sub

    Private Function validateRequiredFields()
        If Me.txtSurname.Text = "" Then
            MsgBox("Apellido es Requerido", vbCritical, "Importante")
            Me.txtSurname.Focus()
            Return False
        End If

        If Me.txtFirstname.Text = "" Then
            MsgBox("Nombres es Requerido", vbCritical, "Importante")
            Me.txtFirstname.Focus()
            Return False
        End If

        If Me.cmbDocumentType.SelectedIndex = -1 Then
            MsgBox("Tipo de Documento es Requerido", vbCritical, "Importante")
            Me.cmbDocumentType.Focus()
            Return False
        End If

        If Me.txtDocument.Text = "" Then
            MsgBox("Documento es Requerido", vbCritical, "Importante")
            Me.txtDocument.Focus()
            Return False
        End If

        Return True
    End Function

    Private Sub txtDocument_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtDocument.KeyPress
        ViewValidators.isInteger(e)
    End Sub

    Private Sub txtHourCost_KeyPress(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtHourCost.KeyPress
        ViewValidators.isFloat(e)
    End Sub

    Private Sub loadFormToEdit(ByVal id As Integer)
        Dim table As Data.DataTable = GenericAdministrator.searchDataById("RESOURCES", "*", id)
        Me.txtFirstname.Text = table.Rows(0)("firstname")
        Me.txtSurname.Text = table.Rows(0)("surname")
        Me.cmbDocumentType.SelectedValue = table.Rows(0)("document_type")
        Me.txtDocument.Text = table.Rows(0)("document_number")
        Me.txtHourCost.Text = table.Rows(0)("hour_cost")
        Me.txtEmail.Text = table.Rows(0)("email")
    End Sub

    Private Function validateDuplicatedResource()
        Dim table As Data.DataTable = ResourceAdministrator.searchDataByFilter(Nothing, Nothing, Nothing, Me.cmbDocumentType.SelectedValue, Me.txtDocument.Text)
        If table.Rows.Count > 0 Then
            MsgBox("El recurso ya existe en la base de datos.", vbCritical, "Importante")
            Return False
        End If
        Return True
    End Function

    Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
        Me.Close()
    End Sub
End Class
